/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ACV;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.event.PrintJobAdapter;


/**
 * @date 07/03/2011 
 * @author Calvin
 */
public class ComicViewerEH {
    


    private Comic currentComic = null;
    private int currentPage = 0;
    private String currentDir = null;
    protected AdvancedComicViewer guiComicViewer = null;

    public ComicViewerEH(AdvancedComicViewer gui) {
        
        this.guiComicViewer = gui;
        this.currentDir = "";
        this.currentPage = 0;
        this.currentComic = null;

    }

    public void pageForward() {
        if(this.currentComic.hasPage(currentPage + 1)) {
            this.currentPage++;
            this.guiComicViewer.imagePanel.image.flush();
            this.guiComicViewer.imagePanel.image = this.currentComic.getPage(currentPage);
            this.guiComicViewer.repaint();
            
        }
        
        
        
    }

    public void pageBack() {
        if(this.currentComic.hasPage(currentPage - 1)) {
            this.currentPage--;
            this.guiComicViewer.imagePanel.image.flush();
            this.guiComicViewer.imagePanel.image = this.currentComic.getPage(currentPage);
            this.guiComicViewer.repaint();
            
        }
        
    }
    
    /**
     * This method is called when the user presses the Scale button.
     * This method scales the currently displayed page to the dimensions of
     * the window at the time the button was pressed.
     */
    public void scalePage() {   
            this.guiComicViewer.repaint();
            this.printPage();
    }
    
    
    public void printPage() {

        PrinterJob printjob = PrinterJob.getPrinterJob();
        printjob.setJobName(" TESSCO CUSTOMER CARD ");
        
        final ImagePanel tempImagePanel = this.guiComicViewer.imagePanel;

        Printable printable = new Printable() {

            @Override
            public int print(Graphics pg, PageFormat pf, int pageNum) {

                if (pageNum > 0) {
                    return Printable.NO_SUCH_PAGE;
                }

                Dimension size = tempImagePanel.getSize();
                BufferedImage bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);

                tempImagePanel.print(bufferedImage.getGraphics());

                Graphics2D g2 = (Graphics2D) pg;
                g2.translate(pf.getImageableX(), pf.getImageableY());
                g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);

                return Printable.PAGE_EXISTS;
            }
        };

        Paper paper = new Paper();
        paper.setImageableArea(0, 0, 153, 243);
        paper.setSize(243, 154);

        PageFormat format = new PageFormat();
        format.setPaper(paper);
        format.setOrientation(PageFormat.LANDSCAPE);

        printjob.setPrintable(printable, format);
        if (printjob.printDialog() == false) {
            return;
        }

        try {
            printjob.print();
        } catch (PrinterException ex) {
            System.out.println("NO PAGE FOUND." + ex);

        }



    }

    public void openComic(String path) {
        
        if (this.currentComic != null) {
            this.closeComic();
        }
        
        this.currentComic = new Comic(path);
        this.currentDir = path;
        if (this.currentComic.hasPage(1)) {
            this.currentPage = 1;
            if(this.guiComicViewer.imagePanel.image != null) {
                this.guiComicViewer.imagePanel.image.flush();
            }
            this.guiComicViewer.imagePanel.image = this.currentComic.getPage(1);
            setButtons(true);
        } else {
            this.closeComic();
            setButtons(false);
        }
        
        this.guiComicViewer.repaint();
        
    }
    
    public void setButtons(boolean flag) {
        this.guiComicViewer.prevBtn.setEnabled(flag);
        this.guiComicViewer.nextBtn.setEnabled(flag);
        this.guiComicViewer.scaleBtn.setEnabled(flag);
        this.guiComicViewer.viewPanel.setVisible(flag);
        this.guiComicViewer.repaint();
        
    }

    public void closeComic() {
        this.guiComicViewer.imagePanel.image.flush();
        this.guiComicViewer.imagePanel.image = null;
        this.guiComicViewer.repaint();
        this.currentComic = null;
        this.currentDir = "";
        this.currentPage = 0;
        this.setButtons(false);
        System.gc();
        
    }
    
}
